﻿using System;
using System.Data.Linq;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Core.Utilities
{
    public static class ImageHelper
    {
        /// <summary>
        /// 将二进制数据转换成图片流
        /// </summary>
        /// <param name="imgBinary">图片二进制数据</param>
        public static Image ToImage(this Binary imgBinary)
        {
            try
            {
                byte[] buffer = imgBinary.ToArray();
                MemoryStream stream = new MemoryStream(buffer);
                return Image.FromStream(stream);
            }
            catch (Exception er)
            {
                throw new Exception("将二进制数据转换成图片时出错");
            }
        }

        /// <summary>
        /// 将图片转换成二进制数据
        /// </summary>
        /// <param name="image">图片</param>
        public static byte[] ToBinary(this Image image)
        {
            var mS = new MemoryStream();
            image.Save(mS, ImageFormat.Bmp);
            return mS.ToArray();
        }

        /// <summary>
        /// 更改图片尺寸
        /// </summary>
        /// <param name="bmp">原始Bitmap</param>
        /// <param name="newW">新的宽度</param>
        /// <param name="newH">新的高度</param>
        /// <returns>处理以后的Bitmap</returns>
        public static Bitmap Resize(this Image bmp, int newW, int newH)
        {
            try
            {
                //新建新的图片并指定大小
                Bitmap newBmp = new Bitmap(newW, newH);

                //根据新的图片绘制
                Graphics g = Graphics.FromImage(newBmp);
                
                //修改类型--高质量二次修改
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //根据已有图片绘制新的图片
                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);

                //释放绘图板
                g.Flush();
                g.Dispose();

                return newBmp;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 合并照片
        /// </summary>
        /// <param name="mainImg">主照片</param>
        /// <param name="subImg">次照片</param>
        /// <returns>Image</returns>
        public static Image MergeImg(this Image mainImg, Image subImg)
        {
            //新建一个宽50Px高60Px的图片
            Bitmap b = new Bitmap(50, 60);

            //根据图片创建绘图板
            Graphics g1 = Graphics.FromImage(b);

            //绘制图片
            g1.DrawImage(subImg, new Rectangle(0, 0, 50, 60), new Rectangle(0, 0, subImg.Width, subImg.Height), GraphicsUnit.Pixel);
            
            //图片重绘质量
            g1.CompositingQuality = CompositingQuality.HighQuality;
            g1.Flush();
            g1.Dispose();
            
            //根据主图片创建绘图板
            Graphics g2 = Graphics.FromImage(mainImg);
            
            //在主图片基础上绘制次图片
            g2.DrawImage(b, new Rectangle(0, 0, b.Width, b.Height));
            
            //图片重绘质量
            g2.CompositingQuality = CompositingQuality.HighQuality;
            g2.Flush();
            g2.Dispose();
            return mainImg;
        }
    }
}
